<?php
class Integral_log_applyAction extends IntesysAction {
	function index(){
        $this->initView();
		$integral_logDB=D('Integral_log');
		$map=$integral_logDB->filter('apply');
		$join=['left join __INTEGRAL_RULE__ t on r.rule_id=t.id','left join __MEMBER__ m on r.member_id=m.id'];
		$field='r.*,m.name,t.name as type_name,m.img_url';	
		$integral_logDB->_list($this->view,$join,$field,$map);
		
       //获取 筛选或添加 所需的积分规则类型，积分类型，部门，所有员工等数据
       $this->assign('memberlist',D('Member')->getallgroupmember2());
       $this->assign('grouplist',D('Member_group')->getGroup());
	   $this->assign('ruletypelist',D('Integral_ruletype')->getRuletype(0));
	   $this->assign('integraltypelist',D('Integral_type')->getType());
	   if($_POST['requestType']==10001100){
           die(json_encode(array('log'=>$list,'statu'=>1)));
       }else{
           $this->display();    
       }
	}
	public function update(){
		if($this->_get('id')){
            $this->dao=$this->dao = D('Integral_log');
            $_POST['id']=$this->_get('id');
            if($this->_get('statu')){
                $_POST['statu_id']=$this->_get('statu');
            }
            //添加分数+修改日志 set Give Integral
            if($this->_get('statu')&&$this->_get('statu')==1){
                $log=$this->dao->where(array('id'=>$this->_get('id')))->find();
                $_POST['__hash__']=$this->_get('__hash__');
                $_POST['changenum']=(int)$log['changenum'];
				$_POST['cid']=trim(str_replace($this->UserInfo,'',$log['cid']),', ');
				if(trim($_POST['cid'])==''){
					$type=M('Integral_type')->where(array('id'=>$log['type_id']))->getField('typename');
					import("@.ORG.Integrals.".$type);
					$class=new $type(session(C('USER_AUTH_KEY')));
					if($class->setGiveIntegral($_POST)){
						$this->success('操作成功', U('Intesys/' . MODULE_NAME . '/index')); 
					}else{
						$this->error('参数错误');    
					}					
				}else{
					unset($_POST['statu_id'],$_POST['changenum']);
					parent::update();
				}  
            }else{
				$_POST['cid']=$_POST['pid'];
            	$_POST['giver_id']=session(C('USER_AUTH_KEY'));
                parent::update();   
            }
		}
	}
	public function insert(){
        if(IS_POST){
            $this->dao = D('Integral_log');
            $_POST['site_id'] = (int)__SITEID__;
            $_POST['time']=strtotime($_POST['date']);
            $_POST['member_id']=session(C('USER_AUTH_KEY'));
            $_POST['statu_id']=3;
			$_POST['cid']=$_POST['pid'];
            #app 接口-> 在父类 返回 状态json
            if($_POST['requestType']==10001110){
                session('requestType',true);    
            }else{
                session('requestType',NULL);    
            }
            parent::insert();
        }
	}
    public function getinfo(){
        if($this->_get('id')){
            $info=D('Integral_log')->where(array('id'=>$this->_get('id')))->find();    
            die(json_encode(array('info'=>$info,'statu'=>1)));    
        }    
    }
	public function delete(){
		if($this->_get('id')){
            $this->dao=D('Integral_log');
            $log=$this->dao->where(array('id'=>$this->_get('id')))->Field('type_id,statu_id')->find();
            if($log['statu_id']==1){
                $type=M('Integral_type')->where(array('id'=>$log['type_id']))->getField('typename');
                import("@.ORG.Integrals.".$type);
                $class=new $type(session(C('USER_AUTH_KEY')));
                if($class->deleteIntegral($this->_get('id'))){
                    $this->success('操作成功', U('Intesys/' . MODULE_NAME . '/index')); 
                }else{
                    $this->error('参数错误');    
                } 
            }else{
			    parent::delete();
            }
		}
	}
}

